/*
 * @lc app=leetcode id=467 lang=cpp
 *
 * [467] Unique Substrings in Wraparound String
 */

// @lc code=start
class Solution {
public:
    int findSubstringInWraproundString(string p) {
        if(p.empty()) return 0;

        vector<int> letter(26,0);
        vector<int> sub(p.size(),0);

        sub[0] = 1;
        letter[p[0]-'a'] = 1;

        for(int i = 1 ;i < p.size(); i ++)        {
            if( p[i] - p[i-1] == 1 || p[i] - p[i-1] == -25 )
                 sub[i] += sub[i-1];
            sub[i] ++;
            int ind = p[i] - 'a';
            letter[ind] = max(letter[ind],sub[i]);
        }

        int sum = 0;
        for(int i=0; i < letter.size() ; i ++)        {
            // cout << letter[i];
            sum += letter[i];
        }
        return sum;
    }
};
// @lc code=end

